﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.Web.Routing;

namespace OAclient.Models
{
    //public class VaildateLoginRoleAttribute : ActionFilterAttribute
    //{
    //    public string Role { set; get; }
    //    public override void OnActionExecuted(ActionExecutedContext filterContext)
    //    {
    //        base.OnActionExecuted(filterContext);
    //    }
    //    public override void OnActionExecuting(ActionExecutingContext filterContext)
    //    {
    //        if (!string.IsNullOrEmpty(Role))
    //        {
    //            if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
    //            {
    //                string redirectOnSuccess = filterContext.HttpContext.Request.RawUrl;
    //                string redirectUrl = string.Format("?ReturnUrl={0}", redirectOnSuccess);
    //                string loginUrl = FormsAuthentication.LoginUrl + redirectUrl;
    //                filterContext.HttpContext.Response.Redirect(loginUrl, true);
    //            }
    //            else
    //            {
    //                //判断是否存在角色
    //                FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
    //                FormsAuthenticationTicket ticket = id.Ticket;
    //                string roles = ticket.UserData;
    //                string[] chkRoles = this.Role.Split(',');
    //                bool isAuthorized = false;
    //                if (Array.IndexOf(chkRoles, roles) > -1)
    //                    isAuthorized = true;
    //                else
    //                    isAuthorized = false;

    //                if (!isAuthorized)
    //                    filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary(new { controller = "Login", action = "LogOn" }));
    //                //throw new UnauthorizedAccessException("你没有权限访问该页面");
    //            }
    //        }
    //        else
    //        {
    //            throw new InvalidOperationException("没有指定角色");
    //        }
    //    }
    //}

    public class VaildateLogin : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            //if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
            UserModel user = (UserModel)filterContext.HttpContext.Session["_usermodel"];
            if (null == user || null == user.user)
            {
                //filterContext.Result = new RedirectToRouteResult("Default", new RouteValueDictionary(new { controller = "Login", action = "LogOn" }));
                //filterContext.RouteData = new RouteData();
                //filterContext.RequestContext.HttpContext.Response.Redirect("Login/LogOn");
                filterContext.RequestContext.HttpContext.Response.Redirect("http://" + filterContext.HttpContext.Request.Url.Host +":"+ filterContext.HttpContext.Request.Url.Port);
                //filterContext.RequestContext.HttpContext.Response.Write("<script>location.href='/Login/LogOn';</script>");
                //filterContext.RequestContext.HttpContext.Response.End();
            }
        }
    }

}
